package com.itheima.service.Impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.config.aspect.Mylog;
import com.itheima.mapper.EmpMapper;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;
    /**
     * 业务层
     * @param page
     * @param pageSize
     * @return
     */
    @Override
    public PageBean selectByPage(Integer page, Integer pageSize) {
        //获取总数据
        Long count=empMapper.count();
        //获取分页的数据
        Integer start =(page-1)*pageSize;
        List<Emp> pageByPageSezi=empMapper.selectByPage(start ,pageSize);
        PageBean pageBean = new PageBean(count,pageByPageSezi);
        return pageBean;

    }

    @Override
    public PageBean selectByPages(String name, String gender, LocalDate begin, LocalDate end, Integer page, Integer pageSize) {
        //调用分页插件 到时候自动帮我添加limit语句
        PageHelper.startPage(page,pageSize);
        //执行分页查询
        List<Emp>empList=empMapper.list(name,gender,begin,end);
        //获取分页结果
        Page<Emp> page1= (Page<Emp>) empList;

        //封装pageBean
        PageBean pageBean = new PageBean(page1.getTotal(), page1.getResult());
        return pageBean;
    }
    @Mylog
    @Override
    public void deleteByIds(List<Integer> ids) {
        empMapper.deleteById(ids);
    }
    @Mylog
    @Override
    public void insertByList(Emp emp) {

        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());

        empMapper.insertByList(emp);
    }
}
